`
https://leetcode.cn/problems/count-unguarded-cells-in-the-grid/
`

/**
 * @param {number} m
 * @param {number} n
 * @param {number[][]} guards
 * @param {number[][]} walls
 * @return {number}
 */
var countUnguarded = function (m, n, guards, walls) {
  let res = m * n - guards.length - walls.length
  const STATE = {
    SAFE: 1, // 表示没被警卫看到
    WATCH: 2, // 表示被警卫看到
    WALL: 3 // 表示墙，警卫也视作墙
  }
  const mat = Array.from({ length: m }, () => new Array(n).fill(STATE.SAFE))
  for (const [x, y] of walls) mat[x][y] = STATE.WALL
  for (const [x, y] of guards) mat[x][y] = STATE.WALL

  const direc = [[1, 0], [-1, 0], [0, 1], [0, -1]]

  // 判断是否越界
  const isValid = (x, y) => {
    return !(x < 0 || y < 0 || x >= m || y >= n)
  }

  // 遍历每一个警卫
  for (const [x, y] of guards) {
    // 遍历四个方向
    for (const [nx, ny] of direc) {
      let i = x, j = y
      // 往当前方向一直搜索
      while (true) {
        i += nx
        j += ny
        // 直到搜到尽头或遇到墙
        if (!isValid(i, j) || mat[i][j] === STATE.WALL) break
        // 更新答案
        if (mat[i][j] !== STATE.WATCH) {
          res--
          mat[i][j] = STATE.WATCH
        }
      }
    }
  }

  return res
};